import { ref, onMounted, onUnmounted } from 'vue';

/**
 * Cesium相机最大缩放距离控制hook
 * 当屏幕宽度大于等于3840时，设置maximumZoomDistance为600000
 * 否则设置为400000
 */
export const useCesiumZoomDistance = () => {
  const isUltraWideScreen = ref(false);

  // 检查屏幕宽度并设置缩放距离
  const updateZoomDistance = () => {
    const screenWidth = window.innerWidth;
    isUltraWideScreen.value = screenWidth >= 3840;

    if (window.viewer && window.viewer.scene && window.viewer.scene.screenSpaceCameraController) {
      window.viewer.scene.screenSpaceCameraController.maximumZoomDistance = isUltraWideScreen.value ? 600000 : 400000;
    }
  };

  // 监听窗口大小变化
  const handleResize = () => {
    updateZoomDistance();
  };

  onMounted(() => {
    // 初始化时设置一次
    updateZoomDistance();

    // 监听窗口大小变化
    window.addEventListener('resize', handleResize);
  });

  onUnmounted(() => {
    // 清理事件监听器
    window.removeEventListener('resize', handleResize);
  });

  return {
    isUltraWideScreen,
    updateZoomDistance
  };
};
